var samples = {};

var selectUrl = null;

samples.onload = function() {
	document.getElementById("reportFrame").contentWindow.document.designMode = 'on';
	document.getElementById("reportFrame").contentWindow.document.open();
	// page button
	$("#btnFirstPage").bind("click", MutiReport.firstPage);
	$("#btnPrevPage").bind("click", MutiReport.prevPage);
	$("#btnNextPage").bind("click", MutiReport.nextPage);
	$("#btnLastPage").bind("click", MutiReport.lastPage);

	//print button
	$("#btnPreview").bind("click", samples.preview);
	$("#btnPrint").bind("click", samples.print);
	//export button
	$("#btnExport").bind("click", samples.showExport);
	$("#btnStartExport").bind("click", samples.startExport);
	$("#btnClose").bind("click", samples.closeExport);
	
	//menu link
	$("#btnColumn").bind("click", samples.getColumnReport);
	$("#btnPercentageColumn").bind("click", samples.getPercentageColumn);
	$("#btnCrosstab").bind("click", samples.getCrosstab);
	$("#btnCrosstab2").bind("click", samples.getCrosstab2);
	$("#groupRow").bind("click", samples.getGroupRow);
	$("#groupRow2").bind("click", samples.getGroupRow2);
	$("#mutiPage").bind("click", samples.getMutiPage);

	$("#timeSeriesChart").bind("click", samples.getTimeSeriesChart);
	$("#ganttChart").bind("click", samples.getGanttChart);
	$("#bar3DChart").bind("click", samples.getBar3DChart);
	$("#pie3DChart").bind("click", samples.getPie3DChart);
	$("#meterChart").bind("click", samples.getMeterChart);
	$("#scatterChart").bind("click", samples.getScatterChart);
	$("#thermometerChart").bind("click", samples.getThermometerChart);
	$("#areaChart").bind("click", samples.getAreaChart);
	$("#mutiAxisChar").bind("click", samples.getMutiAxisChar);

	$("#iReportTemplate").bind("click", samples.getIReportTemplate);
	
};

samples.getColumnReport = function() {
	samples.getReport("getColumnReport.action");
};

samples.getTimeSeriesChart = function() {
	samples.getReport("getTimeSeriesChart.action");
};

samples.getGanttChart = function() {
	samples.getReport("getGanttChart.action");
};

samples.getBar3DChart = function() {
	samples.getReport("getBar3DChart.action");
};

samples.getPie3DChart = function() {
	samples.getReport("getPie3DChart.action");
};

samples.getPercentageColumn = function() {
	samples.getReport("getPercentageColumn.action");
};

samples.getCrosstab = function() {
	samples.getReport("getCrosstab.action");
};

samples.getCrosstab2 = function() {
	samples.getReport("getCrosstab2.action");
};

samples.getGroupRow = function() {
	samples.getReport("getGroupRow.action");
};

samples.getGroupRow2 = function() {
	samples.getReport("getGroupRow2.action");
};

samples.getMeterChart = function() {
	samples.getReport("getMeterChart.action");
};

samples.getScatterChart = function() {
	samples.getReport("getScatterChart.action");
};

samples.getThermometerChart = function() {
	samples.getReport("getThermometerChart.action");
};

samples.getAreaChart = function() {
	samples.getReport("getAreaChart.action");
};

samples.getMutiPage = function() {
	samples.getReport("getMutiPage.action");
};

samples.getMutiAxisChar = function() {
	samples.getReport("getMutiAxisChar.action");
};

samples.getIReportTemplate = function() {
	samples.getReport("getIReportTemplate.action");
};

samples.getReport = function(url) {
	//alert(url);
	
	var timestamp = Date.parse(new Date());
	$.ajax({
		type:"GET",
		dataType:"json",
		url:url + "?timestamp=" + timestamp,
		success:function(jsonData) {
			//alert(jsonData);
			if (jsonData) {
				$("#reportArea").empty().html(jsonData);
				MutiReport.init("reportArea");
				selectUrl = url;
				//samples.hideMorePage();
				MutiReport.switchPage(0);
			} else {
				$("#reportArea").html("can't fetch data from database");
			}
		}
	});
	//document.getElementById("reportFrame").src = url;
};

samples.preview = function() {
	//打印文档对象
	/*
	var reportFrameDoc = document.getElementById("reportFrame").contentWindow.document;
	if (reportFrameDoc.body.innerHTML == "") {
		alert("没有打印的报表");
		return;
	}*/
	//alert(reportFrameDoc.body.innerHTML);
	var myDoc ={
		documents: document,    // 打印页面(div)们在本文档中
		copyrights: '杰创软件拥有版权 www.jatools.com',	// 版权声明,必须 
		classesReplacedWhenPrint: new Array('.page_hidden{display:block}')
	}; 
	jatoolsPrinter.printPreview(myDoc);   // 打印预览
};

samples.print = function() {
	var myDoc ={
		documents: document,    // 打印页面(div)们在本文档中
		copyrights: '杰创软件拥有版权 www.jatools.com'		// 版权声明,必须 
	};
	jatoolsPrinter.print(myDoc, true);   // 打印前弹出打印设置对话框         
};

samples.printWithoutPrompt = function() {
	//打印文档对象
	var myDoc ={
		documents: document,    // 打印页面(div)们在本文档中
		copyrights: '杰创软件拥有版权 www.jatools.com'		// 版权声明,必须 
	};			         
	jatoolsPrinter.print(myDoc, false);  // 不弹出对话框打印
};

samples.hideMorePage = function() {
	for(var i = 2; i < 12; i++) {
		if ($("#page" + i)) {
			$("#page" + i).hide();
		}
	}
};

samples.showExport = function() {
	if (selectUrl) {
		$("#selectExportFileType").show(300);
	}
};

samples.startExport = function() {
	var fileType = $("#fileType").val();
	var timestamp = Date.parse(new Date());
	var url = selectUrl + "?timestamp=" + timestamp + "&opt=export&ftype=" + fileType;
	//alert(url);
	//samples.closeExport();
	window.open(url);
};

samples.closeExport = function() {
	$("#selectExportFileType").hide(300);
};